#include "ThreadFlacScanner.hpp"
#include "FlacTrack.hpp"


ThreadFlacScanner::ThreadFlacScanner(const QDir &dir, QObject *parent):
    QThread(parent),
    m_Iterator(dir, QDirIterator::Subdirectories),
    m_AlbumData(""),
    m_Persistence(),
    m_Stopped(false),
    m_DeepScanEnabled(m_Persistence.getEnableDeepScan()),
    m_ScanIsRunning(false)
{}

void ThreadFlacScanner::run()
{
    m_ScanIsRunning = true;
    qDebug() << "ThreadFlacScan::run";
    Album *album = new Album;
    // Clear list before insert.
    m_AlbumData.clearFlacAlbumList();
    int cnt=0;
    while(!m_Stopped && m_Iterator.hasNext())
    {

        m_Iterator.next();

        if (m_Iterator.fileInfo().isFile()) // File found.
        {
            QString dirPath = m_Iterator.fileInfo().absolutePath();
            QDir myDir(dirPath);

            //if(QFileInfo(entry).suffix().toLower() == "flac")
            if (m_Iterator.fileInfo().absoluteFilePath().endsWith(".flac")) // File is FLAC.
            {
                album->setPath(dirPath);
                album->setName(myDir.dirName());

                // Add track to album
                if (!m_DeepScanEnabled)
                {
                    album->addTrack(m_Iterator.fileName());
                    ++cnt;
                    //qDebug() << "QuickScan! " << m_Iterator.fileName();
                }
                else
                {
                    // qDebug() << "DeepScan! " << m_Iterator.fileName();
                    m_AlbumData.addFlacTrackToAlbum(m_Iterator.fileInfo(), *album);
                    ++cnt;
                }

            }
        }

        // Add Album.
        if (m_Iterator.fileInfo().isDir() && !album->getName().isEmpty())
        {
            m_AlbumData.addAlbum(*album);
            qDebug() << "Album added: " << album->getName() << " Number of Tracks: " << cnt;
            emit sigAlbumFound("FLAC-Album found: " + album->getPath());
            cnt=0;
            delete album;
            album = new Album;
        }

        // This is necessary to catch the last found album.
        if (!m_Iterator.hasNext() && !(*album).getName().isEmpty() )
        {
            m_AlbumData.addAlbum(*album);
            qDebug() << "Last Album added: " << album->getName() << " Number of Tracks: " << cnt;
            emit sigAlbumFound("FLAC-Album found: " + album->getPath());
            cnt=0;
            delete album;
        }
    }
    m_ScanIsRunning=false;
    emit sigFlacSearchFinished();
}


const AlbumDataModel& ThreadFlacScanner::getAlbumData() const
{
    return m_AlbumData.get();
}

bool ThreadFlacScanner::scanIsRunning() const
{
    return m_ScanIsRunning;
}

void ThreadFlacScanner::slotStop()
{
    m_Stopped = true;
}

void ThreadFlacScanner::slotDeepScanEnabled(bool isEnabled)
{
    qDebug() << "ThreadFlacScanner::slotDeepScanEnabled: " << isEnabled;
    QString msg = isEnabled ? "Deep-Scan enabled" : "Quick-Scan enabled";
    emit sigAlbumFound(msg);
    m_DeepScanEnabled=isEnabled;
}

